﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace SoftSource.Common
{
    public static class CollectionExtensions
    {
        /// <summary>
        /// Used to populate a collection without recreating it and therefore destroying any references pointing at it.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <param name="enumerable"></param>
        /// <returns></returns>
        public static bool Populate<T>(this ICollection<T> collection, IEnumerable<T> enumerable)
        {
            try
            {
                if (collection != null)
                {
                    collection.Clear();

                    if (!enumerable.IsNullOrEmpty())
                    {
                        enumerable.Run(collection.Add);
                        return true;
                    }
                }
            }
            catch (Exception exception)
            {
                return false;
            }

            return false;
        }
    }
}